// pages/transOrderList/transOrderList.js
var app = getApp()
var QRCode = require('../../utils/weapp-qrcode.js');
var qrcode1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sessionid:'',
    index1: 0,
    option1: ['已取货', '待付款', '已付款', '已完成', '已收货', '已评价'],
    src: '', //二维码图片地址
    image: '',
    showModel: false,
    list: [],
    baseImgPath: app.globalData.baseImgPath,
    showPjInput: false, //显示评价提交框
    currentTab: "order",
    page:1,
    current:'1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.current)
    var that = this;
    let s=1;
    if (options.current != undefined) {
      that.setData({
        current: options.current
      })
      s=parseInt(options.current)
    }else{
      s=1
    }
    wx.getStorage({
      key: 'sessionId',
      success: function(res) {
        that.setData({
          sessionid: res.data //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionid);
        wx.request({
          url: app.globalData.urlPath + 'ManagerMent/expressList',
          data: {
            sessionid: that.data.sessionid,
            status: s,
            page:that.data.page
          },
          method: "POST",
          success: function(res) {
            console.log(res);
            if (res.data.code == 200) {
              var data = res.data.data
              that.setData({
                list: data
              });
            } else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
              if (res.data.code == 5000) {
                wx.redirectTo({
                  url: '/pages/login/login',
                })
              }
              that.setData({
                list: ''
              });
            }
          }
        })
      },
    })
    qrcode1 = new QRCode('canvas-hidden', {
      text: '123',
      width: 183,
      height: 183,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  //底部导航切换
  handleChangeTab({
    detail
  }) {
    var that=this;
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.reLaunch({
        url: '/pages/transGetList/transGetList',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          wx.showModal({
            title: '是否确认收货?',
            // content: '模态弹窗',
            success: function(res) {
              if (res.confirm) {
                //加载二维码返回数据
                wx.request({
                  url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
                  data: {
                    sessionid: app.globalData.sessionid,
                    qrcode: that.data.qrcode
                  },
                  method: "POST",
                  success: function(res) {
                    console.log(res)
                    if (res.data.code == 200) {
                      wx.showToast({
                        title: res.data.data,
                        icon: 'success',
                        duration: 2000
                      })
                      setTimeout(function(){
                        wx.redirectTo({
                          url: '/pages/transOrderList/transOrderList?current=5',
                        })
                      },1000)
                    } else {
                      wx.showToast({
                        title: res.data.error,
                        icon: 'none',
                        duration: 2000
                      })
                      if (res.data.code == 5000) {
                        wx.redirectTo({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  }
                })
              } else {
                return;
              }

            }
          })
        }
      })
    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/transMy/transMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this;
    self.data.page += 1;
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/expressList',
      data: {
          sessionid: self.data.sessionid,
          status: parseInt(self.data.current),
          page: self.data.page
      },
      method: "POST",
      success: function (res) {
        console.log(res.data.data)
        let list = res.data.data;
        if (res.data.code == 400) {
          self.setData({ showend: true });
          return false
        }
        self.setData({ list: self.data.list.concat(list) })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindchange1({ detail }) {
    var that = this;  
    that.setData({ current: detail.key, page: 1})
   var data = {
      sessionid: that.data.sessionid,
      status: parseInt(detail.key) ,
      page: 1
    }
   wx.request({
      url: app.globalData.urlPath + 'ManagerMent/expressList',
      data:data,
      method: "POST",
      success: function(res) {
        if (res.data.code == 200) {
          var data = res.data.data;
          console.log(data)
          that.setData({
            image: res.data.data
          });
          that.setData({
            list: data
          });
        } else {
          wx.showToast({
            title: res.data.error,
            duration: 2000,
            icon: 'none'
          })
          that.setData({
            list: ''
          });
          if (res.data.code == 5000) {
            wx,
            wx.redirectTo({
              url: '/pages/login/login',
            })
          }
        }
      }
    })
  },
  // bindchange1: function(e) {
  //   var that = this;
  //   var index = e.detail.value
  //   console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
  //   this.setData({
  //     index1: index
  //   })
  //   wx.request({
  //     url: app.globalData.urlPath + 'ManagerMent/expressList',
  //     data: {
  //       sessionid: that.data.sessionid,
  //       status: (parseInt(index) + 1),
  //       page:1
  //     },
  //     method: "POST",
  //     success: function(res) {
  //       if (res.data.code == 200) {
  //         var data = res.data.data;
  //         console.log(data)
  //         that.setData({
  //           image: res.data.data
  //         });
  //         that.setData({
  //           list: data
  //         });
  //       } else {
  //         wx.showToast({
  //           title: res.data.error,
  //           duration: 2000,
  //           icon: 'none'
  //         })
  //         that.setData({
  //           list: ''
  //         });
  //         if (res.data.code == 5000) {
  //           wx,
  //           wx.redirectTo({
  //             url: '/pages/login/login',
  //           })
  //         }
  //       }
  //     }
  //   })
  // },
  //打印二维码
  printCode: function(e) {
    var that = this;
    console.log(e)
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/printwq',
      data: {
        sessionid: that.data.sessionid,
        ftid: e.target.id //订单号
      },
      method: "POST",
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
          if (res.data.code == 5000) {
            wx.redirectTo({
              url: '/pages/login/login',
            })
          }
        }
      }
    })
  },
  onChangeShowModel: function (e) {
    let index = e.currentTarget.dataset.index
    var that = this;
    let list = that.data.list;
    let codeText = list[index].fsercode
    that.setData({
      showModel: (!that.data.showModel),
      showOrderId: list[index].ftid,
    })
    qrcode1.makeCode(codeText)

  },
  //关闭二维码弹窗
  close: function () {
    var that = this;
    that.setData({
      showModel: (!that.data.showModel)
    })
  },
  // 长按保存
  save: function () {
    // console.log('save')
    wx.showActionSheet({
      itemList: ['保存图片'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          qrcode1.exportImage(function (path) {
            wx.saveImageToPhotosAlbum({
              filePath: path,
            })
          })
        }
      }
    })
  },
})